package clue

import (
	"encoding/json"
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/http_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	customerService "git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/customer"
	reportService "git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"
	"github.com/gin-gonic/gin"
	"github.com/goinggo/mapstructure"
)

type CustomerController struct {
	customerService customerService.CustomerServiceIface
	reportService   reportService.ReportServiceIface
	controller.BaseController
}

// NewCustomerController
// 找客户
func NewCustomerController(customerService customerService.CustomerServiceIface,
	reportService reportService.ReportServiceIface,
) *CustomerController {
	return &CustomerController{
		customerService: customerService,
		reportService:   reportService,
	}
}

// RegisterRouterOld 旧的路由，接口需要下线
func (c *CustomerController) RegisterRouterOld(g *gin.RouterGroup) {
	// 备选项
	g.POST("/get-all-options", c.GetAllOptions)
	// 客户动态
	g.POST("/get-feeds", c.GetFeeds)
	// 拿地列表
	g.POST("/take-land", c.TakeLand)
	// 开盘列表
	g.POST("/opened-trading", c.OpenedTrading)
	// 公司下拉选项
	g.POST("/company-option", c.CompanyOption)
	// 校验开发商是否在主档案
	g.POST("/check-developer-main-data", c.CheckDeveloperMainData)
	// 申请反哺开发商主档案接口
	g.POST("/apply-developer-main-data", c.ApplyDeveloperMainData)
}

func (c *CustomerController) SetAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

func (c *CustomerController) GetAllOptions(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	var params = make(map[string][]string)
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	tags := params["tags"]
	res, err := c.customerService.GetAllOptions(ctx, tags)

	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	gfyxModelResponse := &http_client.GfyxModelResponse{}
	//将 map 转换为指定的结构体
	err = mapstructure.Decode(res, gfyxModelResponse)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	if gfyxModelResponse.Error.Exception.Message != "" {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, gfyxModelResponse.Error.Exception.Message)
		return
	}
	c.Success(ctx, res)
}

func (c *CustomerController) GetFeeds(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	res, err := c.customerService.GetFeeds(ctx, params)

	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	gfyxModelResponse := &http_client.GfyxModelResponse{}
	//将 map 转换为指定的结构体
	err = mapstructure.Decode(res, gfyxModelResponse)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	if gfyxModelResponse.Error.Exception.Message != "" {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, gfyxModelResponse.Error.Exception.Message)
		return
	}
	c.Success(ctx, res)
}

func (c *CustomerController) TakeLand(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	rs, err := c.customerService.TakeLand(ctx, params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	c.Success(ctx, rs)
}

func (c *CustomerController) OpenedTrading(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	rs, err := c.customerService.OpenedTrading(ctx, params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	c.Success(ctx, rs)
}

func (c *CustomerController) CompanyOption(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	rs, err := c.customerService.CompanyOption(ctx, params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	c.Success(ctx, rs)
}

func (c *CustomerController) CheckDeveloperMainData(ctx *gin.Context) {
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	rs, err := c.customerService.CheckDeveloperMainData(ctx, params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	c.Success(ctx, rs)
}

func (c *CustomerController) ApplyDeveloperMainData(ctx *gin.Context) {
	var params = make(map[string]interface{})
	err := json.NewDecoder(ctx.Request.Body).Decode(&params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	rs, err := c.customerService.ApplyDeveloperMainData(ctx, params)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	c.Success(ctx, rs)
}
